package com.rem.concurrency.juc.my1Base;


/**
 * 不安全卖票
 *
 * @author Rem
 * @date 2021-03-08
 */

public class UnSafeSale {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        /**
         * 多线程操作同一资源 对于资源来说没有附属条件
         */
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "C").start();
    }
}


/**
 * 资源类
 */
class Ticket {
    private Integer num = 30;

    void sale() {

        if (num > 0) {
            System.out.println(Thread.currentThread().getName() + "--->" + num--);
        }

    }
}
